﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.ComponentModel;

namespace NetLogger
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private Exiting _exiting;

		public MainWindow()
		{
			CollectionViewSource
				members;

			Activation = new NetActivation( this );

			DataContext = this;

			InitializeComponent();

			members = ( CollectionViewSource ) FindResource( "members" );
			members.View.CollectionChanged += new NotifyCollectionChangedEventHandler( OnMemberCollectionChanged );

			Activation.PropertyChanged += new PropertyChangedEventHandler( OnActivationPropertyChanged );
		}

		public NetActivation Activation
		{
			get;
			private set;
		}

		public void OnFind( object sender, ExecutedRoutedEventArgs e )
		{
			CallsignTextBox.Focus();
		}

		private void CanFindExecute( object sender, CanExecuteRoutedEventArgs e )
		{
			e.CanExecute = true;
		}

		public void OnNew( object sender, ExecutedRoutedEventArgs e )
		{
			ReportTextBox.Focus();
		}

		private void CanNewExecute( object sender, CanExecuteRoutedEventArgs e )
		{
			e.CanExecute = true;
		}

		private void OnLoaded( object sender, RoutedEventArgs e )
		{
			if ( !Activation.Connect() )
			{
				MessageBox.Show( Properties.Resources.ErrorConnect, Properties.Resources.ErrorCaption, MessageBoxButton.OK, MessageBoxImage.Error );
				Close();
			}
		}

		private void OnClosing( object sender, System.ComponentModel.CancelEventArgs e )
		{
			Activation.Disconnect( new NetActivation.ExitingFeedbackDelegate( ShowExitingDialog ) );
			if ( _exiting != null )
				_exiting.Close();
			e.Cancel = false;
		}

		private void OnLocationDoubleClick( object sender, MouseButtonEventArgs e )
		{
			Activation.SelectedMember.Location.CommitSelection();
		}

		private void CallsignTextBox_LostFocus( object sender, RoutedEventArgs e )
		{
			Activation.AutoFillMember();
		}

		void OnActivationPropertyChanged( object sender, PropertyChangedEventArgs e )
		{
			if ( e.PropertyName == "SelectedMember" )
			{
				RosterListView.ScrollIntoView( Activation.SelectedMember );
			}
		}

		void OnMemberCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
		{
			object
				scrollToMember;

			if ( Activation.GetScrollToMember( out scrollToMember ) )
				RosterListView.ScrollIntoView( scrollToMember );
		}

		private void ShowExitingDialog()
		{
			_exiting = new Exiting();
			//_exiting.Owner = this;
			_exiting.Show();
		}

		private void TextBox_GotFocus( object sender, RoutedEventArgs e )
		{
			TextBox
				t = sender as TextBox;

			if ( t != null )
			{
				t.SelectAll();
			}
		}

		private void OnQueryLocationDoubleClick( object sender, MouseButtonEventArgs e )
		{
			Activation.QueryLocation.CommitSelection();
		}

		private void NewMessageText_GotFocus( object sender, RoutedEventArgs e )
		{
			Activation.SetDefaultMessageRecipient();
		}
	}
}
